package com.kevin.utility.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 环境工具
 * 
 * @author Random.LJ
 * @Since 2015年1月21日
 * 
 */
public class ContextUtil implements ApplicationContextAware{

	private static ApplicationContext context;
	
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		// TODO Auto-generated method stub
		this.context = context;
	}

	/**
	 * 获取上下文属性ApplicationContext
	 * 
	 * @return
	 */
	public static ApplicationContext getApplicationContext(){
		return context;
	}
	
	/**
	 * 获取context中声明的bean
	 * 
	 * @param cs
	 * @return
	 */
	public static <T> T getContextBean(Class<T> cs){
	    if (null != context){
	        return context.getBean(cs);
	    }
	    return null;
	}
}
